package com.gzhryc.net_marketing.cloud.business.services;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.cloud.business.dao.PayGoodsOrderLogisticsDao;
import com.gzhryc.net_marketing.cloud.business.dao.db.PayGoodsOrderLogistics;
import com.gzhryc.net_marketing.cloud.business.dao.enums.PayGoodsOrderLogisticsEnum;
import com.gzhryc.net_marketing.cloud.logistics.LogisticsInfo;

public class PayGoodsOrderLogisticsService {

    static Logger log = Logger.getLogger(PayGoodsOrderLogisticsService.class);

    public static PayGoodsOrderLogisticsService self() {
        return MultiDBTools.getService(PayGoodsOrderLogisticsService.class, true);
    }

    public static PayGoodsOrderLogisticsService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PayGoodsOrderLogisticsService.class, true);
    }

    PayGoodsOrderLogisticsDao dao;

    public PayGoodsOrderLogisticsService(String jdbcKey) {
        dao = new PayGoodsOrderLogisticsDao(jdbcKey);
    }

    public PayGoodsOrderLogistics getByTradeNo(String tradeNo){
        if(StringUtils.isNotBlank(tradeNo)){
            try {
                return dao.getById(tradeNo);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public PayGoodsOrderLogistics add(PayGoodsOrderLogistics logistics){
        try {
            if(dao.insert(logistics) > 0){
                return logistics;
            }
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean lockUpdate(String tradeNo){
        if(StringUtils.isNotBlank(tradeNo)){
            Conditions condition = new Conditions(PayGoodsOrderLogistics.class);
            condition.and().is("tradeNo",tradeNo);
            condition.and().is("lockState", PayGoodsOrderLogisticsEnum.LockState.Open.index());

            Map<String,Object> data = new HashMap<>();
            data.put("lockState", PayGoodsOrderLogisticsEnum.LockState.Close.index());

            try {
                return dao.update(data,condition) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean openUpdate(String tradeNo){
        if(StringUtils.isNotBlank(tradeNo)) {
            PayGoodsOrderLogistics updateEntity = new PayGoodsOrderLogistics();
            updateEntity.setTradeNo(tradeNo);
            updateEntity.setLockState(PayGoodsOrderLogisticsEnum.LockState.Open.index());

            try {
                return dao.updateNotNull(updateEntity) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean save(String tradeNo, LogisticsInfo logisticsInfo){
        if(StringUtils.isNotBlank(tradeNo) && logisticsInfo != null){
            PayGoodsOrderLogistics updateEntity = new PayGoodsOrderLogistics();
            updateEntity.setTradeNo(tradeNo);
            updateEntity.setLogisticsState(logisticsInfo.getLogisticsState());
            updateEntity.setLogisticsContent(JsonTools.toJson(logisticsInfo));
            updateEntity.setLastUpdateDate(logisticsInfo.getLastUpdateDate());

            try {
                return dao.updateNotNull(updateEntity) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
    
    public boolean deleteByTradeNo(String tradeNo) {
		if(StringTools.isNotBlank(tradeNo)) {
			try {
				if(dao.deleteById(tradeNo) >= 0) {
					return true;
				}
			}catch(SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return false;
	}
}
